/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.SachDAO;
import Entity.Sach;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dark_Knight
 */
public class SachImpl implements SachBUS {

    private SachDAO dao;
    
    public SachImpl() {
        dao = new DAO.SachImpl();
    }
    
    public boolean add(Sach data) {
        return dao.add(data);
    }

    public ArrayList getAll() {
        ResultSet rs = dao.getAll();
        ArrayList list = new ArrayList();
        Sach data;
        try {
            while (rs.next()) {
                data = new Sach(rs.getInt(1), rs.getInt(2), rs.getInt(3), 
                        rs.getInt(4), null, null, null, rs.getInt(5));
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return list;
    }
    
    protected ArrayList convertToList(ResultSet rs) {
        ArrayList list = new ArrayList();
        try {
            while (rs.next()) {
                Object[] data = {
                    rs.getString(1),    //TenSach
                    rs.getString(2),    //TenTacGia
                    rs.getString(3),    //TenTheLoai
                    rs.getString(4),    //TenNhaXuatBan
                    rs.getInt(5),       //NamXuatBan
                    rs.getInt(6),       //GiaBia
                    rs.getInt(7)        //SoLuong                        
                };
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
        }     
        return list;
    }

    public ArrayList findByTenSach(String name) {
        ResultSet rs = dao.findByTenSach(name);
        return convertToList(rs);
    }

    public ArrayList findByNXB(String name) {
        ResultSet rs = dao.findByNXB(name);
        return convertToList(rs);
    }

    public ArrayList findByTheLoai(String name) {
        ResultSet rs = dao.findByTheLoai(name);
        return convertToList(rs);
    }

    public ArrayList findByTacGia(String name) {
        ResultSet rs = dao.findByTacGia(name);
        return convertToList(rs);
    }

    public ArrayList getAllWithTenSach() {
        ResultSet rs = dao.getAllWithTenSach();
        ArrayList list = new ArrayList();
        
        try {
            while (rs.next()) {
                Object[] data = {
                    rs.getInt(1),       //MaSach
                    rs.getInt(2),       //MaDauSach
                    rs.getInt(3),       //GiaBia
                    rs.getInt(4),       //NamXB
                    rs.getInt(5),       //SoLuongTon
                    rs.getString(6),    //TenSach
                    rs.getString(7),    //TenNXB
                };
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(SachImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return list;
    }
    
    public boolean subSoLuongTon(int maSach, int soLuong) {
        return dao.subSoLuongTon(maSach, soLuong);
    }    
}
